// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as sidebar from './sidebar';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

	//注册侧边栏面板的实现
	const sidebarHot = new sidebar.SidebarHot();
	const sidebarSoon = new sidebar.SidebarSoon();
	vscode.window.registerTreeDataProvider("sidebar_hot",sidebarHot);
	vscode.window.registerTreeDataProvider("sidebar_soon",sidebarSoon);
	//注册命令
	vscode.commands.registerCommand("sidebar_item.openWebview",args=>{
		//vscode.window.showInformationMessage(args);
		const panel = vscode.window.createWebviewPanel(
			'webview', // viewType
			 args["movie_name"], // 视图标题，为电影名称
			vscode.ViewColumn.One,
			{
				enableScripts: true, // 启用JS，默认禁用
				retainContextWhenHidden: true // webview被隐藏时保持状态，避免被重置
			}
		);
		//这里只能输入html内容
		let content = getContentHtml(args);
		panel.webview.html = showWebviewContent(args,content);
	    
	});
	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = vscode.commands.registerCommand('movienews.helloWorld', () => {
		// The code you place here will be executed every time your command is executed

		// Display a message box to the user
		vscode.window.showInformationMessage('Hello World from 电影资讯!');
	});

	context.subscriptions.push(disposable);
}
function showWebviewContent(args:any,content:string){
	let result = `
	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
	<div style="height:500px;width: 500px; margin: 50px auto 0;">
	<div><img src="`+args["poster"]+`" style="width: 160px;height: 240px;"> 
	</div>
	<div style="position: relative;top: -265px;left: 180px;">
		<p>
		<h2>`+args["movie_name"]+`</h2>
		</p>
		`+content+`
	</div>
</div>
</body>
</html>`;
return result;
}

function getContentHtml(args:any){
	let content = '';
	for(var i in args) {
		if(!isNaN(Number(i))){
			content+="<p>"+args[i]+"</p>";
		}
	}
	return content;
}
// this method is called when your extension is deactivated
export function deactivate() {}
